//
//  ViewController.m
//  UI-08 HomeWork通讯录
//
//  Created by dllo on 16/7/25.
//  Copyright © 2016年 YMJ. All rights reserved.
//

#import "ViewController.h"
#import "DetailViewController.h"
#import "AddPersonViewController.h"
#define kTableView @"nimeide"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,AddPersonViewControllerDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *nameArray;
@property (nonatomic, strong) NSMutableArray *ageArray;
@property (nonatomic, strong) NSMutableArray *genderArray;
@property (nonatomic, strong) NSMutableArray *phoneNumberArray;
@property (nonatomic, strong) NSMutableDictionary *nDiction;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor brownColor];
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableView];
    [self createData];
    [self createNavigationRightbarButton];
    _tableView.contentInset = UIEdgeInsetsMake(0,0, 64, 0);
    
}
- (void)createNavigationRightbarButton {
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPerson:)];
}
// 添加学生
- (void)addPerson:(UIBarButtonItem *)button {
    AddPersonViewController *addVC = [[AddPersonViewController alloc] init];
    [self.navigationController pushViewController:addVC animated:YES];
    // 签订协议
    addVC.delegate = self;
}
// 实现协议方法
- (void)sendValue:(NSMutableDictionary *)dic {
    self.nDiction = [NSMutableDictionary dictionaryWithDictionary:dic];
    [self.nameArray addObject:_nDiction[@"name"]];
    [self.nameArray addObject:_nDiction[@"age"]];
    [self.nameArray addObject:_nDiction[@"gender"]];
    [self.nameArray addObject:_nDiction[@"phoneNumber"]];
    [self.tableView reloadData];
    
}
- (void)createData {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160710" ofType:@"plist"];
    NSArray *array = [NSArray arrayWithContentsOfFile:path];

    self.nameArray = [NSMutableArray array];
    self.ageArray = [NSMutableArray array];
    self.genderArray = [NSMutableArray array];
    self.phoneNumberArray = [NSMutableArray array];
    for (NSDictionary *dic in array) {
        NSString *nameString = dic[@"name"];
        
        [_nameArray addObject:nameString];
        [_ageArray addObject:dic[@"age"]];
        [_genderArray addObject:dic[@"gender"]];
        [_phoneNumberArray addObject:dic[@"phoneNumber"]];
    }

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _nameArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableView forIndexPath:indexPath];
    cell.backgroundColor = [UIColor yellowColor];
    cell.textLabel.text = _nameArray[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    [self.navigationController pushViewController:detailVC animated:YES];
    detailVC.name = _nameArray[indexPath.row];
    detailVC.age = (NSInteger )_ageArray[indexPath.row];
    detailVC.gender = _genderArray[indexPath.row];
    detailVC.phoneNumber = _phoneNumberArray[indexPath.row];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
